package fr.esiag.isidis.epp.search;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import fr.esiag.isidis.epp.generatedClasses.CourbeDeCharge;
import fr.esiag.isidis.epp.generatedClasses.PointCdc;
import fr.esiag.isidis.epp.generatedClasses.PointConso;
import fr.esiag.isidis.epp.generatedClasses.Query;
import fr.esiag.isidis.epp.generatedClasses.QueryExportCdc;
import fr.esiag.isidis.epp.util.Utils;

@Service
public class PointCdcService {

	private static Logger logger = Logger.getLogger(PointCdcService.class);

	@Autowired
	private PointCdcRepository repository;

	public List<PointCdc> makeIndividuel(Query q){
		return repository.individualQuery(q);
	}

	public List<PointCdc> makeCumul(Query q){
		return repository.cumulQuery(q);
	}

	public List<PointCdc> makeMoyenne(Query q){
		return repository.moyenneQuery(q);
	}

	public PointConso  traiteQuery(QueryExportCdc qec){
		CourbeDeCharge cdc = new CourbeDeCharge();
		List<Query> listq = qec.getQuery();
		List<PointCdc> lpcdc;
		String id = listq.get(0).getIDSite();
		for(Query q : listq){
			if(q.getType().equals(q.getType().INDIVIDUEL)){
				lpcdc =  makeIndividuel(q);
				System.out.println(lpcdc);
				for(PointCdc l : lpcdc) {
					cdc.getPointCdc().add(l);
				}
				logger.info("call individual query");
			}

			if(q.getType().equals(q.getType().CUMULE)){
				lpcdc = makeCumul(q);
				for(PointCdc l : lpcdc) {
					cdc.getPointCdc().add(l);
				}
				logger.info("call cumul query");
			}

			if(q.getType().equals(q.getType().MOYENNE)){
				lpcdc = makeMoyenne(q);
				for(PointCdc l : lpcdc) {
					cdc.getPointCdc().add(l);
				}
				logger.info("call moyenne query");
			}
		}
		PointConso pc = new PointConso();
		pc.setCourbeDeCharge(cdc);
		pc.setIDSite(id);
		return pc;
	}
}
